package com.ews.onlineexchange.service;

import java.util.List;

import javax.annotation.security.RolesAllowed;
import javax.enterprise.inject.Model;
import javax.inject.Inject;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;

import com.ews.onlineexchange.model.DataEntity;
import com.ews.onlineexchange.model.ProfessionUser;
import com.ews.onlineexchange.model.QualificationCert;
import com.ews.onlineexchange.model.QualificationClass;
import com.ews.onlineexchange.model.ServiceSupplier;

@Model
@RolesAllowed( "profession" )
public class AllCertList_admin extends EntityQuery<QualificationCert> {

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;

	@Inject
	UserService					userService;
	@Inject
	QualificationClassService	qualificationClassService;

	@Override
	protected List<Predicate> getRestrictions( CriteriaBuilder cb, Root<QualificationCert> entity ) {
		List<Predicate> restrictions = super.getRestrictions( cb, entity );
		// Join<QualificationTemplate, QualificationClass> qClass =
		// entity.join("qualificationTemplates").join("qualificationClass");
		// ProfessionUser loginedUser = (ProfessionUser) userService.findLoginedUser();
		// restrictions.add(cb.equal(qClass.get("auditDepartment"),
		// loginedUser.getServiceDepartment()));
		Join<QualificationCert, ServiceSupplier> supplier = entity.join( "serviceSupplier" );
		restrictions.add( cb.equal( supplier.get( "delFlag" ), DataEntity.DEL_FLAG_NORMAL ) );
		String supplierName = (String) getParameterValues().get( "supplierName" );
		if ( !StringUtils.isBlank( supplierName ) )
			restrictions.add( cb.like( supplier.get( "cnname" ), getStringRestriction( supplierName ) ) );
		String auditStatus = (String) getParameterValues().get( "auditStatus" );
		if ( StringUtils.isNotBlank( auditStatus ) ) {
			if ( "2".equals( auditStatus ) ) {
				restrictions.add( cb.isNull( entity.get( "auditResult" ) ) );
			} else if ( "0".equals( auditStatus ) ) {
				restrictions.add( cb.isFalse( entity.<Boolean>get( "auditResult" ) ) );
			} else {
				restrictions.add( cb.isTrue( entity.<Boolean>get( "auditResult" ) ) );
			}
		}
		// if(!"2".equals(auditStatus)) {
		// if(StringUtils.isBlank(auditStatus)) {
		// restrictions.add(cb.isNull(entity.get("auditResult")));
		// }else if ("0".equals(auditStatus)) {
		// restrictions.add(cb.isFalse(entity.<Boolean>get("auditResult")));
		// }else {
		// restrictions.add(cb.isTrue(entity.<Boolean>get("auditResult")));
		// }
		// }
		Join<QualificationCert, ProfessionUser> professionUser = null;
		String auditorName = (String) getParameterValues().get( "auditorName" );
		if ( !StringUtils.isBlank( auditorName ) && !StringUtils.isBlank( auditStatus ) ) {
			professionUser = entity.join( "professionUser" );
			if ( professionUser != null )
				restrictions.add( cb.like( professionUser.get( "cnname" ), getStringRestriction( auditorName ) ) );
		}

		Join<QualificationCert, QualificationClass> qclass = entity.join( "qualificationClass" );
		String qualificationClassId = (String) getParameterValues().get( "qualificationClassId" );
		if ( !StringUtils.isBlank( qualificationClassId ) ) {
			QualificationClass qc = qualificationClassService.find( qualificationClassId );
			restrictions.add( cb.equal( qclass, qc ) );
		}
		return restrictions;
	}

	public AllCertList_admin() {
		setMaxResults( MAX_RESULTS_NORMAL );
		setOrder( "allotedDate ASC,auditResult DESC,valid asc,createDate ASC" );
	}

}
